<?php
include "otamu_config.php";

$layout = "dam_layout";
define("_VIEWPATH_","views/dam/");

if (!isset($_GET['act'])) $_GET['act'] = ''; 

class Dam extends ADOdb_Active_Record
{
	var $_table = 'dams';
}


switch ($_GET['act']) {
	case "new" :
		break;

	case "create" :
		if ($name=='') exitError("Sila Masukkan Nama Water Dam");
		//check if already exist group
		$sql = "SELECT name FROM dams WHERE name ilike '$_REQUEST[name]'";
		$n = pg_num_rows(pg_query($sql));
		
		if ($n>0) exitError("Water Dam already exists.");

		$obj = new Dam();

		$obj->name            = $_REQUEST['name'];
		$obj->build    		  = $_REQUEST['build'];
		$obj->upgrading		  = $_REQUEST['upgrading'];
		$obj->operator_id	  = $_REQUEST['operator_id'];
		$obj->handle_id	  	  = $_REQUEST['handle_id'];
		$obj->type_id	  	  = $_REQUEST['type_id'];
		$obj->catchment 	  = $_REQUEST['catchment'];
		$obj->impoundment	  = $_REQUEST['impoundment'];
		$obj->storage	  	  = $_REQUEST['storage'];
		$obj->district_id	  = $_REQUEST['district_id'];
		$obj->state_id	  	  = $_REQUEST['state_id'];
		
		//print_r($obj);exit;

		if (!$obj->save()) { 
			$_SESSION['flash'] = $obj->ErrorMsg();
		}else {
			$_SESSION['flash'] = 'Water Dam has been added';
		}
		header("Location: dam.php?act=list");
		break;
		
	case "edit" :
		if (!isset($id) || $id=='') {
			exitError("Water Dam not found.");
		}
		$sql = "SELECT * FROM dams WHERE id='$id'";
		$res = pg_query($sql);
		$dat = pg_fetch_object($res);
		$name 			= $dat->name;
		$build			= $dat->build;
		$upgrading 		= $dat->upgrading;
		$catchment 	 	= $dat->catchment;
		$impoundment	= $dat->impoundment;
		$storage		= $dat->storage;
		
		break;

	case "update" :
		if (!isset($id) || $id=='') {
			exitError("Water Dam not found.");
		}
		$ig = new Dam();

		$obj =  $ig->Find("id='$_REQUEST[id]'");
				
		if ($obj) { $obj = $obj[0];}
		//print_r($obj);exit;

		$obj->name            = $_REQUEST['name'];
		$obj->build			  = $_REQUEST['build'];
		$obj->upgrading		  = $_REQUEST['upgrading'];
		$obj->operator_id	  = $_REQUEST['operator_id'];
		$obj->handle_id	  	  = $_REQUEST['handle_id'];
		$obj->type_id	  	  = $_REQUEST['type_id'];
		$obj->catchment       = $_REQUEST['catchment'];
		$obj->impoundment	  = $_REQUEST['impoundment'];
		$obj->storage	  = $_REQUEST['storage'];
		$obj->district_id	  = $_REQUEST['district_id'];
		$obj->state_id	  	  = $_REQUEST['state_id'];

		if (!$obj->save()) { 
			$_SESSION['flash'] = $obj->ErrorMsg();
		}else {
			$_SESSION['flash'] = 'Water Dam has been updated';
		}
		header("Location: dam.php?act=list");
		
		break;

	case "destroy" :
		if (isset($id)) {
			/*$check = "SELECT DISTINCT(id) FROM dams WHERE id='$id'";
			$n = pg_num_rows(pg_query($check));
			if ($n>0) {
				exitError("This group is still in use and cannot be deleted.");
			}*/
			$sql = "DELETE FROM dams WHERE id='$id'";
			$res = pg_query($sql);
			$_SESSION['flash'] = 'Water Dam has been deleted';
			header("Location: dam.php?act=list");
		}
		else {
			echo "water dam not found $btn";exit;
		}
		break;
	
	default :
		$act = "list";
		$sql = "SELECT w.id,w.name as dam_name,o.name as operator_name,w.upgrading as dam_upgrade,w.build as dam_build FROM dams w 
		        LEFT JOIN operators o ON o.id=w.operator_id
				ORDER BY w.name
		        ";	
		$res = pg_query($sql);
		
}

include _VIEWPATH_."$layout.php";

?>
